////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                           BailterRay                                       //
//                    Pip Robbins & Zhao Zhang                                //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
class BailterRayEvilBot extends UTBot;

var bool bStarted;
var vector StartLoc;
var rotator LastRotation;
var BailterRayPawn PC;
var bool bFrozen;
var bool bSelfDestruct;
var SoundCue SoundOnDestroy;
var SoundCue AlertSound;
var ParticleSystem ParticlesOnDestroy;
var ParticleSystemComponent Explosion;
var ParticleSystem SparkEffect;

const SELF_DESTRUCT_TIME            = 5; // Time till BOOM!

function rotator GetAdjustedAimFor(Weapon W, vector StartFireLoc)
{
	return Rotation;
}

function Initialize(float InSkill, const out CharacterInfo BotInfo)
{
	local UTPlayerReplicationInfo CPRI;
	local CustomCharData MyBotCharData;

	// copy visual properties
	CPRI = UTPlayerReplicationInfo(PlayerReplicationInfo);
    MyBotCharData = (class'UTCustomChar_Data'.static.CharDataFromString("EvilRobot,EVRB,evilRobot_head,NONE,NONE,NONE,evilRobot_torso,NONE,evilRobot_arms,evilRobot_thighs,evilRobot_boots,F,F"));
	super.Initialize(InSkill, BotInfo);

	CPRI.SetCharacterData(MyBotCharData);
}

event Tick( float DeltaTime )
{
    forEach VisibleCollidingActors(class'BailterRayPawn', PC, 1900)
    {
        if(PC.IsHumanControlled())
            break;
    }

    if(PC != none && !IsInState('SelfDestruct'))
    {
        if((Abs(Pawn.Location.X - PC.Location.X)) < 750 && PC.IsHumanControlled())
        {
            GotoState('FrozenMovement');
            Focus = PC;
            FireWeaponAt(PC);
        }
    }
    if(PC != none && bSelfDestruct)
    {
       Destination = PC.Location;
       GoToState('SelfDestruct', 'Move');
    }
}

simulated function GotoFrozen()
{
    bFrozen = true;
}

function bool FireWeaponAt(Actor A)
{
    if(bSelfDestruct)
       return false;

	if ( A == None )
		A = Enemy;
	if ( (A == None) || (Focus != A) )
		return false;
	Focus = A;

    return WeaponFireAgain(false);
}

function NotifyTakeHit(Controller InstigatedBy, vector HitLocation, int Damage, class<DamageType> damageType, vector Momentum)
{

    if(Pawn.health < 40 && Pawn.health > 0 && !IsInState('SelfDestruct')){
	   GotoState('SelfDestruct', 'Begin');
	}
	LastUnderFire = WorldInfo.TimeSeconds;
	if ( (instigatedBy != None) && (instigatedBy != self) )
		damageAttitudeTo(instigatedBy, Damage);
}

function Explode()
{
	// Fire particles
	if(ParticlesOnDestroy != None)
	{
		Explosion = WorldInfo.MyEmitterPool.SpawnEmitter(ParticlesOnDestroy, Pawn.Location, Pawn.Rotation);
		Explosion.SetScale(1);
	}

	// Play sound
	if(SoundOnDestroy != None)
	{
	    SoundOnDestroy.VolumeMultiplier = 0.9;
		PlaySound(SoundOnDestroy, true);
	}
	HurtRadius(30, 180, Class'BailterRayDmgType_Flame', 50000, Pawn.Location,,, true);
	HurtRadius(30, 180, Class'BailterRayDmgType_Flame', 50000, Pawn.Location,,, true);

    Pawn.SetHidden(true);

    GotoState('Dead');
}

function AttachSparkEffect()
{
	local array<ParticleSystemComponent> PSC;
	local array<name> AllBoneNames;
	local rotator SparkRotation;
	local name BoneName;
	local int i;

	Pawn.Mesh.GetBoneNames(AllBoneNames);
	for(i = 0; i < AllBoneNames.length - 1; i++)
    {
       BoneName = AllBoneNames[i];
       SparkRotation.Pitch = Rand(65536);
       SparkRotation.Roll = Rand(65536);
       SparkRotation.Yaw = Rand(65536);
       PSC[i] = WorldInfo.MyEmitterPool.SpawnEmitter(SparkEffect, Pawn.Location, SparkRotation);
	   if(PSC[i] != none)
       {
          PSC[i].SetScale(0.3);
	      PSC[i].SetAbsolute(false, true, false);
	      Pawn.Mesh.AttachComponent(PSC[i], BoneName);
	      PSC[i].SetTranslation(vect(0.0, 0.0, 0.0));
       }
    }
}

function PlayAlertSound()
{
	// Play sound
	if(AlertSound != None)
	{
	    AlertSound.VolumeMultiplier = 2.0;
	    AlertSound.PitchMultiplier = 1.0;
		PlaySound(AlertSound, true);
	}
    setTimer(1.4, false, 'SecondAlert');
    setTimer(2.4, false, 'ThirdAlert');
    setTimer(3.2, false, 'FourthAlert');
    setTimer(4.0, false, 'FithAlert');
    setTimer(4.6, false, 'SixthAlert');
}
function SecondAlert()
{
	// Play sound
	if(AlertSound != None)
	{
	    AlertSound.VolumeMultiplier = 3.0;
	    AlertSound.PitchMultiplier = 1.3;
		PlaySound(AlertSound, true);
	}
}

function ThirdAlert()
{
	// Play sound
	if(AlertSound != None)
	{
	    AlertSound.VolumeMultiplier = 4.0;
	    AlertSound.PitchMultiplier = 1.6;
		PlaySound(AlertSound, true);
	}
}

function FourthAlert()
{
	// Play sound
	if(AlertSound != None)
	{
	    AlertSound.VolumeMultiplier = 5.0;
	    AlertSound.PitchMultiplier = 1.9;
		PlaySound(AlertSound, true);
	}
}

function FithAlert()
{
	// Play sound
	if(AlertSound != None)
	{
	    AlertSound.VolumeMultiplier = 6.0;
	    AlertSound.PitchMultiplier = 2.2;
		PlaySound(AlertSound, true);
	}
}

function SixthAlert()
{
	// Play sound
	if(AlertSound != None)
	{
	    AlertSound.VolumeMultiplier = 7.0;
	    AlertSound.PitchMultiplier = 2.5;
		PlaySound(AlertSound, true);
	}
}

function fallDown()
{
   local UTPawn P;

   P = UTPawn(Pawn);
   P.bCanRagDoll = true;
   P.ForceRagdoll();
   P.bCanRagDoll = false;
}

state SelfDestruct
{
	function startSelfDestruct()
	{
	   bFrozen = false;
       FallDown();
       AttachSparkEffect();
       PlayAlertSound();
       Pawn.Health = Pawn.HealthMax / 2;
       setTimer(SELF_DESTRUCT_TIME, false, 'Explode');
       bSelfDestruct = true;
	}

Begin:
   startSelfDestruct();
   if(PC != none){
      Destination = PC.Location;
      MoveTo(Destination, PC, false);
   }

Move:
   MoveTo(Destination, PC, false);
}

defaultproperties
{
    ScriptedFireMode=0
    bFrozen=false
    bSelfDestruct=false
    SoundOnDestroy=SoundCue'A_Vehicle_Goliath.SoundCues.A_Vehicle_Goliath_Explode'
    AlertSound=SoundCue'A_Vehicle_Viper.Cue.A_Vehicle_Viper_SelfDestructCue'
    ParticlesOnDestroy=ParticleSystem'VH_Raptor.Effects.P_Raptor_RocketExplosion'
    SparkEffect=ParticleSystem'Envy_Level_Effects_2.DM_HeatRay.P_PowerWire_Sparks_01'

    /////////////////////////////
    //Good Spark Effect Options//
    //////////////////////////////////////////////////////////////////
    //GP_Ons_PowerCore_Death.Effects.P_Ons_PowerCore_Armbreak_Linger//
    //Envy_Level_Effects_2.DM_HeatRay.P_PowerWire_Sparks_01         //
    //////////////////////////////////////////////////////////////////
}
